<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App extends MY_Controller {

	public function __construct(){
		parent::__construct();
		$this->user_id = $this->session->userdata('user_id');
		$this->user_name = $this->session->userdata('user_name');
		$this->dept_id = $this->session->userdata('dept_id');
		$this->dept_name = $this->session->userdata('dept_name');
	}
	
	/*首页显示*/
	public function index(){
		$data['settings_site'] = $this->user_m->settings_site();

		//$this->get_all_menus();
		//$data['menus'] = $this->get_parent_array(0);
		//print_r($this->acl->get_parent_array());
		$data['menus'] = $this->acl->get_parent_array();
		$data['notify'] = $this->user_m->getlistdata_main();	
		
		$this->load->view('index',$data);

	}
	/*左侧菜单列表*/
	function _menus_post($pid){
		//$this->get_all_menus();
		
		$data['menus'] = $this->acl->get_child_array($pid);
		echo json_encode($data['menus']);  
	}

	//在线人员
	function online(){
		$timeoutSeconds = 20; //设置的时间
		$numberOfUsers = 0; //用户数
		
		$currentTime = time(); 
		$timeout = $currentTime - $timeoutSeconds; //当前时间减去设置过期时间

		if($this->user_id){ 
			$this->db->delete('users_online', array('user_id' => $this->user_id)); 
			$data = array(
				'timestamp' => $currentTime ,
				'user_id' => $this->user_id ,
				'user_name' => $this->user_name,
				'dept_id' => $this->dept_id,
				'dept_name' => $this->dept_name
			);
			$this->db->insert('users_online', $data); 
			//echo $this->db->last_query();//输出随后一次查询的语
		} 
		
		$this->db->where('timestamp <', $timeout); 
		$this->db->delete('users_online'); 
		//echo $this->db->last_query();//输出随后一次查询的语

		$this->db->distinct();
		$this->db->select('user_id');
		$query = $this->db->get('users_online');
		$row = $query->num_rows();
		echo json_encode($row);  
	}
	//在线人员视图
	function online_view(){
		$this->load->view('online_view');
	}
	//在线人员JSON数据
	function _online_data_post(){
		$query = $this->db->get('users_online');
		$listdata = $query->result();
		foreach ($listdata as $v){
			$rowdata[] = array("user_id"=>$v->user_id,"user_name"=>$v->user_name,"dept_id"=>$v->dept_id,"dept_name"=>$v->dept_name,"timestamp"=>$v->timestamp);
		}

		$listdata = array("total"=>count($rowdata),'rows'=>$rowdata);
		echo json_encode($listdata); 
	}
	
	//通讯录视图
	function contacts_view(){
		$this->load->view('contacts_view');
	}
	
	//通讯录视图数据
	function _contacts_data_post(){
		$rowdata = array();
		$row = $this->user_m->userlist();

		foreach ($row as $v){
			if($v->user_name == 'admin'){
				continue;
			}else{
				$rowrole = $this->user_m->rolelist($v->role_id);
				$rowdept = $this->user_m->deptlist($v->dept_id);
				$rowdata[] = array("user_id"=>$v->user_id,"user_name"=>$v->user_actual,"dept_name"=>$rowdept[0]['text'],"role_name"=>$rowrole[0]->role_name,"user_sex"=>$v->user_sex,"user_birthday"=>$v->user_birthday,"user_mobile"=>$v->user_mobile,"user_qq"=>$v->user_qq);
			}
		}
		$listdata = array("total"=>count($rowdata),'rows'=>$rowdata);
		echo json_encode($listdata); 
	}

}
?>